package cn.tedu.charging.device.service.impl;


import cn.tedu.charging.device.dao.mapper.GunMapper;
import cn.tedu.charging.device.dao.mapper.PileMapper;
import cn.tedu.charging.device.dao.mapper.StationAllMapper;
import cn.tedu.charging.device.dao.mapper.StationMapper;
import cn.tedu.charging.device.pojo.po.GunInfoPO;
import cn.tedu.charging.device.pojo.po.PileInfoPO;
import cn.tedu.charging.device.pojo.po.StationPO;
import cn.tedu.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.service.DeviceService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {
    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam gunStatusUpdateParam) {
        GunInfoPO gunInfoPO = new GunInfoPO();
        //set gun_status = gunStatusUpdateParam.getStatus()
        gunInfoPO.setGunStatus(gunStatusUpdateParam.getStatus());

        UpdateWrapper updateWrapper = new UpdateWrapper();
        // where id = gunStatusUpdateParam.getGunId()
        updateWrapper.eq("id",gunStatusUpdateParam.getGunId());

        //update xxx set gun_status = gunStatusUpdateParam.getStatus()  where id = gunStatusUpdateParam.getGunId()
        gunMapper.update(gunInfoPO,updateWrapper);
        return true;
    }

    @Resource
    private GunMapper gunMapper;

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private PileMapper pileMapper;

    @Override
    public StationInfoVO getStationInfo(Integer gunId) {
        //一个个的分别查，理论上需要用3次连接，性能相对一次查询，会较差
        //如果在高并发情况下，建议用3次，避免锁表 代码复杂多写了，sql简单

        // 目前你们接触的项目的并发不会很高，建议用 一次查询，代码简单，sql不简单

        //return getStationInfoVO(gunId);
        StationPO stationInfoPO = getStationInfoByGunId(gunId);
        if (stationInfoPO != null) {
            StationInfoVO stationInfoVO = new StationInfoVO();
            stationInfoVO.setStationId(stationInfoPO.getId());
            stationInfoVO.setStationName(stationInfoPO.getStationName());
            stationInfoVO.setOperatorId(stationInfoPO.getOperatorId());
            return stationInfoVO;
        }
        return null;
    }



    @Nullable
    private StationInfoVO getStationInfoVO(Integer gunId) {
        log.debug("获取枪信息:入参{}", gunId);
        GunInfoPO gunInfoPO = gunMapper.selectById(gunId);
        log.debug("获取枪信息:入参{},结果:{}", gunId,gunInfoPO);
        if (gunInfoPO != null) {
            Integer pileId = gunInfoPO.getPileId();
            log.debug("获取桩信息:入参{}",pileId);
            PileInfoPO pileInfoPO = pileMapper.selectById(pileId);
            log.debug("获取桩信息:入参{},结果:{}",pileId,pileInfoPO);
            if (pileInfoPO != null) {
                Integer stationId = pileInfoPO.getStationId();
                log.debug("获取场站信息:入参{}",stationId);
                StationPO stationInfoPO = stationMapper.selectById(stationId);
                log.debug("获取场站信息:入参{},结果:{}",stationId,stationInfoPO);
                if (stationInfoPO != null) {
                    StationInfoVO stationInfoVO = new StationInfoVO();
                    stationInfoVO.setStationId(stationId);
                    stationInfoVO.setStationName(stationInfoPO.getStationName());
                    stationInfoVO.setOperatorId(stationInfoPO.getOperatorId());
                    return stationInfoVO;
                }
            }
        }
        return null;
    }



    @Autowired
    private StationAllMapper stationAllMapper;

    private StationPO getStationInfoByGunId(Integer gunId) {
        return stationAllMapper.getStationInfoByGunId(gunId);
    }
}
